import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../views/Login/Login.vue'
// import Reg from '../views/Reg/Reg.vue'
// import Main from '../views/Main/Main.vue'
// import Home from '../views/Menu/Home/Home.vue'
// import userInfo from '../views/Menu/user/userInfo.vue'
import store from '../store'
// import UserAvatar from '@/views/Menu/user/userAvatar.vue'
// import UserPwd from '@/views/Menu/user/userPwd.vue'
// import ArtCate from '@/views/Menu/Article/Article.vue'
// import ArtList from '@/views/Menu/Article/ArtList.vue'
Vue.use(VueRouter)

// 把下面的代码粘贴到路由模块中对应的位置，即可防止路由报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/Login.vue')
  },
  {
    path: '/reg',
    name: 'Reg',
    component: () => import('../views/Reg/Reg.vue')
  },
  {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main/Main.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('../views/Menu/Home/Home.vue')
      },
      {
        path: 'user-info',
        name: 'userInfo',
        component: () => import('../views/Menu/user/userInfo.vue')
      },
      {
        path: 'user-avatar',
        name: 'UserAvatar',
        component: () => import('@/views/Menu/user/userAvatar.vue')
      },
      {
        path: 'user-pwd',
        name: 'UserPwd',
        component: () => import('@/views/Menu/user/userPwd.vue')
      },
      {
        path: 'art-cate',
        name: 'ArtCate',
        component: () => import('@/views/Menu/Article/Article.vue')
      },
      {
        path: 'art-list',
        name: 'ArtList',
        component: () => import('@/views/Menu/Article/ArtList.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/' && !store.state.token) {
    Vue.prototype.$message.error('请登录')
    next('/login')
  }
  next()
})

export default router
